﻿using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectJobAlert : DataAccessBase
    {
        public SelectJobAlert()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectJobAlert.ToString();
        }

        #region GetJobAlert
        public List<JobAlert> GetJobAlert()
        {
            DataBaseHelper dbHelper;
            DataSet dsJobAlert;

            try
            {
                SelectJobAlertParameters selectJobAlertParameters = new SelectJobAlertParameters(this.JobseekerID, this.JobAlertID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectJobAlertParameters.Parameters;
                dsJobAlert = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsJobAlert);
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region SQL parameters for selecting job alert by id
        public class SelectJobAlertParameters
        {
            private SqlParameter[] _parameters;

            public SelectJobAlertParameters(Guid jobSeekerID, long jobAlertID)
            {
                this.JobSeekerID = jobSeekerID;
                this.JobAlertID = jobAlertID;
                Build();
            }

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobSeekerID", this.JobSeekerID),
                    new SqlParameter("@JobAlertID",  this.JobAlertID),
                };

                Parameters = parameters;
            }

            public Guid JobSeekerID { get; set; }
            public long JobAlertID { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetList
        private List<JobAlert> GetList(DataSet dsJobAlert)
        {
            List<JobAlert> jobAlerts;
            try
            {
                jobAlerts = (from n in dsJobAlert.Tables[0].AsEnumerable()
                             select new JobAlert
                                   {
                                       JobSeekerID = n.Field<Guid>("JobSeekerID"),
                                       JobAlertID = n.Field<long>("JobAlertID"),
                                       AlertName = n.Field<string>("AlertName"),
                                       CategoryID = n.Field<int>("CategoryID"),
                                       FrequencyID = n.Field<int>("Frequency"),
                                       CountryID = n.Field<int>("CountryID"),
                                       CityID = n.Field<int>("CityID"),
                                       SalaryRangeID = n.Field<int>("SalaryRangeID"),
                                       WorkTypeID = n.Field<int>("WorkTypeID"),
                                       Skills = n.Field<string>("Skill"),
                                       Keyword = n.Field<string>("Keyword"),
                                       CreatedDate = n.Field<DateTime>("CreatedDate"),
                                       IsActive = n.Field<bool>("IsActive")
                                   }).ToList();

                return jobAlerts;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion

        public Guid JobseekerID { get; set; }

        public long JobAlertID { get; set; }
    }
}

